﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.Mvc;
using XQ.SP.Model;

namespace XM.Controllers.Sys
{
    public class PMController : Controller
    {

        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public FileStreamResult DownFile(string filePath, string fileName, long id = 0)
        {
            string absoluFilePath = Server.MapPath("~") + filePath;
            return File(new FileStream(absoluFilePath, FileMode.Open), "application/octet-stream", Server.UrlEncode(fileName));
        }


        #region 图片UpdateFileStud
        HelpWeb.ImagesHelp imagesHelper = new HelpWeb.ImagesHelp();

        #region 上传图片
        /// <summary>
        /// 上传单张图片
        /// </summary>
        /// <param name="from"></param>
        /// <returns></returns>
        [HttpPost]
        public string UpdateImageOne(string btnName="")
        {
            var file = Request.Files["Filedata"];

            if (file != null)
            {
                string fileT = "";
                fileT = file.FileName;
                if (fileT.Length > 20)
                {
                    fileT = fileT.Substring(0, 20);
                }
                fileT = fileT.Replace("|", "");
                //fileName = fileName.Substring(fileName.Length - 4);
                fileT = fileT.Substring(0, fileT.LastIndexOf("."))
                    + DateTime.Now.ToString("yyyyMMddHHmmssff") + fileT.Substring(fileT.LastIndexOf("."));
                XQ.Common.Images img = new XQ.Common.Images();
                string savePath = img.GetPath();
                string uploadPath = Server.MapPath("~" + savePath);

                file.SaveAs(uploadPath + fileT);
                img.MakeThumbnail(uploadPath + fileT, uploadPath + "S2000" + fileT, 2000, 2000, "HW");
                img.MakeThumbnail(uploadPath + fileT, uploadPath + "S" + fileT, 600, 600, "HW");
                img.MakeThumbnail(uploadPath + fileT, uploadPath + "S160" + fileT, 160, 160, "HW");
                img.MakeThumbnail(uploadPath + fileT, uploadPath + "S80" + fileT, 80, 80, "HW");
                try
                {
                    XQ.Common.Files.DeleteFile(Server.MapPath(uploadPath + fileT));
                }
                catch
                {
                    try { XQ.Common.Files.DeleteFile(uploadPath + fileT); }
                    catch { }
                }

                //imagesHelper.AddStr(saveRoot + "S" + fileName + "|", uid);

                return savePath + "S" + fileT + "|" + btnName;
            }
            else
            {
                return "";
            }
        }
        /// <summary>
        /// 上传多张图片
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="btnName"></param>
        /// <returns></returns>
        [HttpPost]
        public string UpdateImageN(long uid = 0, string btnName = "", int SysTap = 0, int FilesTap=0)
        {
            var file = Request.Files["Filedata"];

            if (file != null)
            {
                string fileT = "";
                fileT = file.FileName;
                if (fileT.Length > 20)
                {
                    fileT = fileT.Substring(0, 20);
                }
                fileT = fileT.Replace("|", "");
                //fileName = fileName.Substring(fileName.Length - 4);
                fileT = fileT.Substring(0, fileT.LastIndexOf("."))
                    + DateTime.Now.ToString("yyyyMMddHHmmssff") + fileT.Substring(fileT.LastIndexOf("."));
                XQ.Common.Images img = new XQ.Common.Images();
                string savePath = img.GetPath();
                string uploadPath = Server.MapPath("~" + savePath);

                file.SaveAs(uploadPath + fileT);
                img.MakeThumbnail(uploadPath + fileT, uploadPath + "S2000" + fileT, 2000, 2000, "HW");
                img.MakeThumbnail(uploadPath + fileT, uploadPath + "S" + fileT, 600, 600, "HW");
                img.MakeThumbnail(uploadPath + fileT, uploadPath + "S160" + fileT, 160, 160, "HW");
                img.MakeThumbnail(uploadPath + fileT, uploadPath + "S80" + fileT, 80, 80, "HW");
                try
                {
                    XQ.Common.Files.DeleteFile(Server.MapPath(uploadPath + fileT));
                }
                catch
                {
                    try { XQ.Common.Files.DeleteFile(uploadPath + fileT); }
                    catch { }
                }

                imagesHelper.AddStr(savePath + "S" + fileT + "|", uid, FilesTap, SysTap);

                return savePath + "S" + fileT + "|" + btnName;
            }
            else
            {
                return "";
            }
        }

        #endregion

        #region 上传文件
        /// <summary>
        /// 上传单个文件
        /// </summary>
        /// <param name="from"></param>
        /// <returns></returns>
        [HttpPost]
        public string UpdateFileOne(string btnName = "")
        {
            var file = Request.Files["Filedata"];

            if (file != null)
            {
                string fileT = "";
                fileT = file.FileName;
                //fileName = fileName.Substring(fileName.Length - 4);
                if (fileT.Length > 20)
                {
                    fileT = fileT.Substring(0, 20);
                }
                fileT = fileT.Replace("|", "");
                fileT = fileT.Substring(0, fileT.LastIndexOf("."))
                    + DateTime.Now.ToString("yyyyMMddHHmmssff") + fileT.Substring(fileT.LastIndexOf("."));
                XQ.Common.Images img = new XQ.Common.Images();
                string savePath = img.GetPath();
                string uploadPath = Server.MapPath("~" + savePath);

                file.SaveAs(uploadPath + fileT);
                //img.MakeThumbnail(root + fileName, root + "S2000" + fileName, 2000, 2000, "HW");
                //img.MakeThumbnail(root + fileName, root + "S" + fileName, 600, 600, "HW");
                //img.MakeThumbnail(root + fileName, root + "S160" + fileName, 160, 160, "HW");
                //img.MakeThumbnail(root + fileName, root + "S80" + fileName, 80, 80, "HW");
                //try
                //{
                //    XQ.Common.Files.DeleteFile(Server.MapPath(root + fileName));
                //}
                //catch
                //{
                //    try { XQ.Common.Files.DeleteFile(root + fileName); }
                //    catch { }
                //}

                //imagesHelper.AddStr(saveRoot + "S" + fileName + "|", uid);

                return savePath  + fileT + "|" + btnName;
            }
            else
            {
                return "";
            }
        }
        /// <summary>
        /// 上传多个文件
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="btnName"></param>
        /// <returns></returns>
        [HttpPost]
        public string UpdateFileN(long uid = 0, string btnName = "", int SysTap = 0, int FilesTap = 0)
        {
            var file = Request.Files["Filedata"];

            if (file != null)
            {
                string fileT = "";
                fileT = file.FileName;
                if (fileT.Length > 20)
                {
                    fileT = fileT.Substring(0,20);
                }
                fileT = fileT.Replace("|", "");
                //fileName = fileName.Substring(fileName.Length - 4);
                fileT = fileT.Substring(0, fileT.LastIndexOf("."))
                    + DateTime.Now.ToString("yyyyMMddHHmmssff") + fileT.Substring(fileT.LastIndexOf("."));
                XQ.Common.Images img = new XQ.Common.Images();
                string savePath = img.GetPath();
                string uploadPath = Server.MapPath("~" + savePath);

                file.SaveAs(uploadPath + fileT);
                //img.MakeThumbnail(root + fileName, root + "S2000" + fileName, 2000, 2000, "HW");
                //img.MakeThumbnail(root + fileName, root + "S" + fileName, 600, 600, "HW");
                //img.MakeThumbnail(root + fileName, root + "S160" + fileName, 160, 160, "HW");
                //img.MakeThumbnail(root + fileName, root + "S80" + fileName, 80, 80, "HW");
                //try
                //{
                //    XQ.Common.Files.DeleteFile(Server.MapPath(root + fileName));
                //}
                //catch
                //{
                //    try { XQ.Common.Files.DeleteFile(root + fileName); }
                //    catch { }
                //}

                imagesHelper.AddStr(savePath + fileT + "|", uid, FilesTap, SysTap);

                return savePath + fileT + "|" + btnName;
            }
            else
            {
                return "";
            }
        }

        #endregion
        /// <summary>
        /// 只上传文件不处理
        /// </summary>
        /// <param name="from"></param>
        /// <returns></returns>
        [HttpPost]
        public string UpLoadFileOnly(long uid = 0)
        {
            //if (UsersInfo.GetUsersID() == "" | UsersInfo.GetUsersID() == "0")
            //{
            //    return "";
            //}
            var file = Request.Files["Filedata"];

            if (file != null)
            {
                string fileT = "";
                fileT = file.FileName;
                string strOldName = file.FileName;

                if (fileT.Length > 10)
                {
                    fileT = fileT.Substring(fileT.Length - 10);
                }
                fileT = DateTime.Now.ToString("yyyyMMddHHmmssffff") + fileT.Substring(fileT.LastIndexOf("."));
                XQ.Common.Images img = new XQ.Common.Images();
                string savePath = img.GetPath();
                string uploadPath = Server.MapPath("~" + savePath);

                file.SaveAs(uploadPath + fileT);
                //img.MakeThumbnail(root + fileName, root + "S1000" + fileName, 1000, 1000, "HW");
                //img.MakeThumbnail(root + fileName, root + "S" + fileName, 400, 400, "HW");
                //img.MakeThumbnail(root + fileName, root + "S160" + fileName, 160, 160, "HW");
                //img.MakeThumbnail(root + fileName, root + "S80" + fileName, 80, 80, "HW");
                //try
                //{
                //    XQ.Common.Files.DeleteFile(Server.MapPath(root + fileName));
                //}
                //catch
                //{
                //    try { XQ.Common.Files.DeleteFile(root + fileName); }
                //    catch { }
                //}

                //imagesHelper.AddStr(saveRoot + fileName + "|", uid);

                //itemRe item = new itemRe();
                //item.Name = strOldName;
                //item.Path = saveRoot + fileName;

                //return Json(item, JsonRequestBehavior.AllowGet);
                return savePath + fileT;
            }
            else
            {
                return "";
                //return Json("", JsonRequestBehavior.AllowGet);
            }
        }



        /// <summary>
        /// 上传图片TextBox 编辑框
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UpdateImageTextBox(long uid = 0)
        {
            var file = Request.Files["imgFile"];

            if (file != null)
            {
                string fileT = "";
                fileT = file.FileName;
                if (fileT.Length > 10)
                {
                    fileT = fileT.Substring(fileT.Length - 10);
                }
                fileT = DateTime.Now.ToString("yyyyMMddHHmmssffffff") + fileT.Substring(fileT.Length - 4);
                XQ.Common.Images img = new XQ.Common.Images();
                string savePath = img.GetPath();
                string uploadPath = Server.MapPath("~" + savePath);

                file.SaveAs(uploadPath + fileT);
                #region 判断是否为图片 如果不是图片直接返回文件位置
                string tt = uploadPath + fileT;
                string s = tt.Substring(tt.LastIndexOf(".") + 1).ToLower();
                if (s != "jpg" & s != "jpge" & s != "png" & s != "bmp" & s != "jpeg" & s != "gif")
                {
                    Hashtable hash1 = new Hashtable();
                    hash1["error"] = 0;
                    hash1["url"] = savePath + fileT;
                    return Json(hash1, "text/html;charset=UTF-8");
                }
                #endregion

                img.MakeThumbnail(uploadPath + fileT, uploadPath + "B1000" + fileT, 1000, 1000, "HW");
                img.MakeThumbnail(uploadPath + fileT, uploadPath + "B400" + fileT, 400, 100, "HW");
                try
                {
                    XQ.Common.Files.DeleteFile(Server.MapPath(uploadPath + fileT));
                }
                catch
                {
                    try { XQ.Common.Files.DeleteFile(uploadPath + fileT); }
                    catch { }
                }

                Hashtable hash = new Hashtable();
                hash["error"] = 0;
                hash["url"] = savePath + "B1000" + fileT;

                if (uid != 0)
                {
                    imagesHelper.AddStr(savePath + "B1000" + fileT + "|", uid);
                }

                return Json(hash, "text/html;charset=UTF-8"); ;
            }
            else
            {
                Hashtable hash = new Hashtable();
                hash["error"] = 0;
                hash["url"] = "请选择文件";
                return Json(hash);
            }
        }

        /// <summary>
        /// 删除图片
        /// </summary>
        /// <param name="fileNameStr"></param>
        /// <returns></returns>
        public JsonResult DeleteImage(string fN, long uid = 0, int SysTap = 0, int FilesTap=0)
        {
            imagesHelper.DeleteStr(fN, uid, FilesTap, SysTap);
            //imagesHelper.DeleteStr(fileNameStr + "|", uid);
            return Json("true", JsonRequestBehavior.AllowGet);
        }

        #endregion
    }
}
